package com.leruan.aasimp.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.leruan.aasimp.constant.UsedCarConstant;
import com.leruan.aasimp.dao.ReplyDao;
import com.leruan.aasimp.entity.Reply;
import com.leruan.aasimp.entitydto.ReplyDto;
import com.leruan.aasimp.util.Validators;

public class ReplyDaoImpl extends HibernateDaoSupport implements ReplyDao {

    public void insertReply(Reply reply) {
        Session session = this.getSession();
        session.save(reply);
    }

    public List<Reply> findReplyByPostId(String postId) {
        Session session = this.getSession();
        String hql = "from Reply where mainpostId=? order by replyDate asc";
        return session.createQuery(hql).setString(0, postId).list();
    }

    public void deleteReplyByPostId(String postId) {
        Session session = this.getSession();
        String hql = "delete from Reply where mainpostId=?";
        session.createQuery(hql).setString(0, postId).executeUpdate();
    }

    public List<ReplyDto> findReplyByPostId(String postId, Map<String, String> map, int pageNo) {
        Session session = this.getSession();

        StringBuffer sb = new StringBuffer(
                "select r.id,r.replyId,r.mainpostId,r.userId,r.replyContent,r.replyDate,r.isshow ,b.boardName,m.postTitle,f.username"
                        + " from Reply r,Board b,Forumuser f,Mainpost m where r.boardId = b.boardId and r.mainpostId=m.postId and "
                        + "r.userId = f.userId and r.mainpostId=?");
        if (null != map) {
            if (!Validators.isEmpty(map.get("userId"))) {
                sb.append(" and r.userId='" + map.get("userId") + "'");
            }
            if (!Validators.isEmpty(map.get("postDateStart"))) {
                sb.append(" and r.replyDate > " + map.get("postDateStart"));
            }
            if (!Validators.isEmpty(map.get("postDateEnd"))) {
                sb.append(" and r.replyDate <" + map.get("postDateEnd"));
            }
        }
        sb.append("order by r.replyDate asc");
        Query query = session.createQuery(sb.toString());
        int first = (pageNo - 1) * UsedCarConstant.PAGE_SIZE;
        query.setString(0, postId);
        query.setFirstResult(first);
        query.setMaxResults(UsedCarConstant.PAGE_SIZE);
        List list = query.list();
        List<ReplyDto> replyList = new ArrayList<ReplyDto>();
        for (int i = 0; i < list.size(); i++) {
            ReplyDto reply = new ReplyDto();
            Object[] object = (Object[]) list.get(i);
            reply.setId((Long) object[0]);
            reply.setReplyId((String) object[1]);
            reply.setMainpostId((String) object[2]);
            reply.setUserId((String) object[3]);
            reply.setReplyContent((String) object[4]);
            reply.setReplyDate((Date) object[5]);
            reply.setIsshow((Long) object[6]);
            reply.setBoardName((String) object[7]);
            reply.setPostTitle((String) object[8]);
            reply.setUsername((String) object[9]);
            replyList.add(reply);
        }
        return replyList;
    }

    public Long findReplyTotal(Map<String, String> map) {

        Session session = this.getSession();
        String boardId = map.get("boardId");
        StringBuffer sb = new StringBuffer("select count(id) from Reply where boardId=? ");
        if (null != map) {
            if (!Validators.isEmpty(map.get("userId"))) {
                sb.append(" and userId='" + map.get("userId") + "'");
            }
            if (!Validators.isEmpty(map.get("postDateStart"))) {
                sb.append(" and replyDate > " + map.get("postDateStart"));
            }
            if (!Validators.isEmpty(map.get("postDateEnd"))) {
                sb.append(" and replyDate <" + map.get("postDateEnd"));
            }
        }
        return (Long) session.createQuery(sb.toString()).setString(0, boardId).uniqueResult();
    }

    public List<ReplyDto> findReply(Map<String, String> map, int pageNo) {
        Session session = this.getSession();

        StringBuffer sb = new StringBuffer(
                "select r.id,r.replyId,r.mainpostId,r.userId,r.replyContent,r.replyDate,r.isshow ,b.boardName,m.postTitle,f.username"
                        + " from Reply r,Board b,Forumuser f,Mainpost m where r.boardId = b.boardId and r.mainpostId=m.postId and "
                        + "r.userId = f.userId");
        if (null != map) {
            if (!Validators.isEmpty(map.get("userId"))) {
                sb.append(" and r.userId='" + map.get("userId") + "'");
            }
            if (!Validators.isEmpty(map.get("postDateStart"))) {
                sb.append(" and r.replyDate > " + map.get("postDateStart"));
            }
            if (!Validators.isEmpty(map.get("postDateEnd"))) {
                sb.append(" and r.replyDate <" + map.get("postDateEnd"));
            }
        }
        sb.append(" order by r.replyDate asc");
        Query query = session.createQuery(sb.toString());
        int first = (pageNo - 1) * UsedCarConstant.PAGE_SIZE;
        query.setFirstResult(first);
        query.setMaxResults(UsedCarConstant.PAGE_SIZE);
        List list = query.list();
        List<ReplyDto> replyList = new ArrayList<ReplyDto>();
        for (int i = 0; i < list.size(); i++) {
            ReplyDto reply = new ReplyDto();
            Object[] object = (Object[]) list.get(i);
            reply.setId((Long) object[0]);
            reply.setReplyId((String) object[1]);
            reply.setMainpostId((String) object[2]);
            reply.setUserId((String) object[3]);
            reply.setReplyContent((String) object[4]);
            reply.setReplyDate((Date) object[5]);
            reply.setIsshow((Long) object[6]);
            reply.setBoardName((String) object[7]);
            reply.setPostTitle((String) object[8]);
            reply.setUsername((String) object[9]);
            replyList.add(reply);
        }
        return replyList;
    }

    public List<Reply> findReplyByUserId(String userId) {
        Session session = this.getSession();
        String hql = "from Reply where userId=?";
        return session.createQuery(hql).setString(0, userId).list();
    }

    public void updateReply(Reply reply) {
        getHibernateTemplate().update(reply);
    }

    /**
     * 
     */
    public Long findReplyNumberById(String userId) {
        Session session = this.getSession();
        String hql = "select count(*) from Reply where userId=?";
        return (Long) session.createQuery(hql).setString(0, userId).uniqueResult();
    }

}
